package com.qianxx.component.activity.image.pull_to_refresh_view_page;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.qianxx.component.R;
import com.qianxx.component.activity.base.BaseActivity;
import com.qianxx.viewlibrary.image.pullTorefreshviewpager.PullToRefreshBase;
import com.qianxx.viewlibrary.image.pullTorefreshviewpager.PullToRefreshViewPager;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PullToRefreshViewPagerActivity extends BaseActivity implements
        PullToRefreshBase.OnRefreshListener<ViewPager> {
    @BindView(R.id.pull_refresh_viewpager)
    PullToRefreshViewPager pull_refresh_viewpager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_to_refresh_view_pager);
        ButterKnife.bind(this);

        pull_refresh_viewpager.setOnRefreshListener(this);

        ViewPager vp = pull_refresh_viewpager.getRefreshableView();
        vp.setAdapter(new SamplePagerAdapter());
    }

    @Override
    public void onRefresh(PullToRefreshBase<ViewPager> refreshView) {
        new GetDataTask().execute();
    }

    static class SamplePagerAdapter extends PagerAdapter {

        private static int[] sDrawables = {R.drawable.p1, R.drawable.p2,
                R.drawable.p3, R.drawable.p4, R.drawable.p5, R.drawable.p6,
                R.drawable.p7, R.drawable.p8};

        @Override
        public int getCount() {
            return sDrawables.length;
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(container.getContext());
            imageView.setImageResource(sDrawables[position]);

            container.addView(imageView, ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);

            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }

    private class GetDataTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            pull_refresh_viewpager.onRefreshComplete();
            super.onPostExecute(result);
        }
    }

    @Override
    public String setTitle() {
        return "PullToRefreshViewPager";
    }
}